<?php

namespace App\Http\Controllers\Api\v2;

use App\Http\Controllers\ApiController;
use App\Models\Item;
use App\Models\Shop;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class IndexController extends ApiController
{
    public function index(){

    }

    public function tuijian(){

        $offset = intval($this->request->input('offset'));
        $count = intval($this->request->input('count'));
        !$count && $count = 20;

        $items = DB::table('item_push AS p')
            ->leftJoin('item AS i', 'i.itemid', '=', 'p.itemid')
            ->where(['i.on_sale'=>1])->orderByDesc('p.id')->offset($offset)->limit($count)
            ->groupBy('i.itemid')->get(['i.*'])->map(function ($item){
                if ($item->sold > 10000) {
                    $item->sold = round($item->sold/10000, 1).'万';
                }
                $item->thumb = image_url($item->thumb);
                $item->image = image_url($item->image);
                return $item;
            });

        return ajaxReturn(['items'=>$items]);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function youxuan(){
        $offset = intval($this->request->input('offset'));
        $count = intval($this->request->input('count'));
        !$count && $count = 20;

        $items = DB::table('item_push AS p')
            ->leftJoin('item AS i', 'i.itemid', '=', 'p.itemid')
            ->where(['p.groupid'=>3,'i.on_sale'=>1])->orderByDesc('p.id')->offset($offset)->limit($count)
            ->groupBy('i.itemid')->get(['i.*'])->map(function ($item){
                if ($item->sold > 10000) {
                    $item->sold = round($item->sold/10000, 1).'万';
                }
                $item->thumb = image_url($item->thumb);
                $item->image = image_url($item->image);
                return $item;
            });

        return ajaxReturn(['items'=>$items]);
    }

    public function food()
    {
        $offset = intval($this->request->input('offset'));
        $count = intval($this->request->input('count'));
        !$count && $count = 20;
        $items = Item::where('shop_id', '1838')->where('on_sale',1)
            ->offset($offset)->limit($count)->orderByDesc('sold')->get()->map(function ($item){
                $item->thumb = image_url($item->thumb);
                $item->image = image_url($item->image);
                return $item;
            });

        $shop = Shop::where('shop_id', '1838')->first();
        $shop->logo = image_url($shop->logo);

        return ajaxReturn(['items'=>$items, 'shop'=>$shop]);
    }

    public function nutritious()
    {
        $shopIds = [1838,1633];
        $shops = [];
        Shop::whereIn('shop_id', $shopIds)->get()->map(function ($shop) use (&$shops){
            $shop->logo = image_url($shop->logo);
            $shop->items = [];
            $shops[$shop->shop_id] = $shop->toArray();
        });

        $prefix = DB::getTablePrefix();
        $query = DB::table(DB::raw($prefix."item as i"))
            ->where(DB::raw("(select count(*) from {$prefix}item where shop_id=i.shop_id and itemid<i.itemid)"),'<', 3);

        //echo $query->toSql();exit();
        $query->whereIn('shop_id', $shopIds)->orderByDesc('itemid')->get()->map(function ($item) use (&$shops){
            $item->thumb = image_url($item->thumb);
            $item->image = image_url($item->image);
            $shops[$item->shop_id]['items'][] = $item;
        });

        return ajaxReturn(['shops'=>array_values($shops)]);
    }

    public function fupin()
    {
        $offset = intval($this->request->input('offset'));
        $count = intval($this->request->input('count'));
        !$count && $count = 20;

        $items = DB::table('item_push AS p')
            ->leftJoin('item AS i', 'i.itemid', '=', 'p.itemid')
            ->where(['p.groupid'=>3,'i.on_sale'=>1])->orderByDesc('p.id')->offset($offset)->limit($count)
            ->groupBy('i.itemid')->get(['i.*'])->map(function ($item){
                if ($item->sold > 10000) {
                    $item->sold = round($item->sold/10000, 1).'万';
                }
                $item->thumb = image_url($item->thumb);
                $item->image = image_url($item->image);
                return $item;
            });

        return ajaxReturn(['items'=>$items]);
    }

    public function checklogin()
    {
        return ajaxReturn([
            'user'=>Auth::user()
        ]);
    }

    public function test()
    {
//        return ajaxReturn([
//            'items'=>$this->request->input()
//        ]);
        return ajaxReturn(['user'=>Auth::user()]);
    }
}
